//
//  XianJiListViewController.m
//  MyFishHelper
//
//  Created by EmberWei on 2018/8/14.
//  Copyright © 2018年 EmberWei. All rights reserved.
//

#import "XianJiListViewController.h"
#import "XianjiCell.h"
#import "XianjiTestViewController.h"

@interface XianJiListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSArray *offerList;
@end

@implementation XianJiListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self reqeust];
    self.title = @"请选择";
}

-(void)loadDataOfferList
{
    WEAK(self)
    [Api apiOfferListWithHandler:^(ResponseOfferIndex *result, NSError *error) {
        STRONG(self)
        if (error) {
            [TSMessage showNotificationWithTitle:@"网络错误"
                                        subtitle:@"请检查网络,稍后再试"
                                            type:TSMessageNotificationTypeError];
            return;
        }
        if (![result.code isEqualToString:@"0"]) {
            [TSMessage showNotificationWithTitle:@"错误"
                                        subtitle:result.msg
                                            type:TSMessageNotificationTypeError];
            return;
        }
        if (result.lists.offer_fish.count) {
            self.offerList = result.lists.offer_fish;
            [self.collectionView reloadData];
        }
    }];
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    OfferFishItem *item = self.offerList[indexPath.item];
    XianjiCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XianjiCell" forIndexPath:indexPath];
    cell.lbLevel.text = FORMAT(@"L%@",item.level);
    cell.lbName.text = item.name;
    [cell.icon sd_setImageWithURL:[NSURL URLWithString:item.img]];
    CGFloat f  = [item.equal_eth floatValue];
    NSString *eth = FORMAT(@"%0.3fETH",f);
    cell.lbEth.text = eth;
    return cell;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return  [self.offerList count];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [UIView beginAnimations:@"animationID" context:nil];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationRepeatAutoreverses:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:[collectionView cellForItemAtIndexPath:indexPath] cache:YES];
    [UIView commitAnimations];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        XianjiTestViewController *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"XianjiTestViewController"];
        OfferFishItem *item = self.offerList[indexPath.item];
        vc.item = item;
        [self.navigationController pushViewController:vc animated:YES];
    });
}


-(void)showLoginAlert
{
    RAlertView *alert = [[RAlertView alloc] initWithStyle:ConfirmAlert];
    alert.headerTitleLabel.text = @"Token过期";
    alert.theme = RGB16(0X1abc9c);
    NSMutableString *string = [[NSMutableString alloc]initWithString:@"您在其他地方登录,请重新登录"];
    alert.contentTextLabel.attributedText = [TextHelper attributedStringForString:string lineSpacing:5];;
    [alert.confirmButton setTitle:@"好的" forState:UIControlStateNormal];
    WEAK(self)
    alert.confirm = ^(){
        STRONG(self)
        [self dismissViewControllerAnimated:YES completion:nil];
    };
}

-(void)reqeust
{
    WEAK(self)
    [Api apiFishMap:^(ResponseFishMap *result, NSError *error) {
        STRONG(self)
        if (error) {
            [MFHUDManager showError:@"请检查网络,稍后再试"];
            return;
        }
        if (![result.code isEqualToString:@"0"]) {
            if ([result.code isEqualToString:@"20000"]) {
                [self showLoginAlert];
                return;
            }
            [MFHUDManager showError:result.msg];
            return;
        }
        if ([result.lists count] == 0) {
            [MFHUDManager showError:@"钓鱼渔场列表为空"];
            return;
        }
        [self loadDataOfferList];
    }];
}


@end
